/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.h2t.loveplace.views;

import com.h2t.loveplace.data.LocationTest;
import com.h2t.loveplace.list.PlaceListRenderer;
import com.h2t.loveplace.list.PlaceModel;
import com.h2t.loveplace.main.LoveplaceMidlet;
import com.h2t.loveplace.models.Place;
import com.h2t.loveplace.utils.PlaceStore;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import java.util.Vector;

/**
 *
 * @author PHAN THAI
 */
public class MainView extends Form implements ActionListener {

    private Vector placelist;
    private LoveplaceMidlet midlet;
    private List list;
    private Command cmdExit;
    private Command cmdAdd;
    private Command cmdAboutUs;
    private Command cmdGuid;
    
  //  private FooterContainer fc;

    public MainView(LoveplaceMidlet midlet) {
        this.midlet = midlet;
      //  setLayout(new BorderLayout());
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        initOtherComponent();
        list.addActionListener(new ListActionListener());
        setTitle("My Place");
    }

    private void initOtherComponent() {
        placelist = PlaceStore.getAllPlace();
        if(placelist == null) {
            placelist = new Vector();
        }
        placelist.addElement(LocationTest.dfplace1);
        placelist.addElement(LocationTest.dfplace2);
        list = new List();
        if (placelist != null) {
            list.setModel(new PlaceModel(placelist));
            list.setListCellRenderer(new PlaceListRenderer());
        } 
        addComponent(list);

        cmdExit = new Command("Exit");
        cmdAdd = new Command("Add");
        cmdAboutUs = new Command("About Us");
        cmdGuid = new Command("Guide");

        addCommand(cmdAdd);
       // addCommand(cmdExit);
        addCommand(cmdAboutUs);
        addCommand(cmdGuid);
        setBackCommand(cmdExit);
        setDefaultCommand(cmdAdd);
        addCommand(cmdExit);
        addCommandListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == cmdExit) {
            midlet.destroyApp(true);
        } else if (ae.getCommand() == cmdAdd) {
            midlet.showNewPlaceView();
        } else if (ae.getCommand() == cmdAboutUs) {
            midlet.showAboutUs();
        } else if (ae.getCommand() == cmdGuid) {
            midlet.showGuideView();
        }
    }

    public void show() {       
        placelist = PlaceStore.getAllPlace();
        if (placelist != null) {
            list.setModel(new PlaceModel(placelist));
            list.setListCellRenderer(new PlaceListRenderer());
        }
        super.show();
     //   refreshTheme();
    }

    class ListActionListener implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            midlet.currentPlace = (Place) list.getSelectedItem();
            // midlet.getMap().setPlace(place.getLat(), place.getLon());
           // midlet.getViewDetaiPlace().setPlace(place);
            midlet.showDetailPlaceView();
        }
    }
}
